#include "vbb_Queue.h"
#include <string.h>

void vbb_Message_Init(VBB_Message * Message)
{
    Message->length = 0;  
    memset(Message->buff, 0, MESSAGE_SIZE);
}

void vbb_Message_AddByte(VBB_Message * Message, char Byte)
{
    if (Message->length + 1 < MESSAGE_SIZE - 1)
    {
        Message->buff[Message->length] = Byte;
        Message->length++;
    }
}

void vbb_DataQueue_Init(VBB_DAT_Queue * Queue, char * Buffer, unsigned int BufferSize)
{
    Queue->head = Queue->tail = 0;
    Queue->buffer = Buffer;
    Queue->bufferSizeMinusOne = BufferSize - 1;
}

void vbb_DataQueue_AddByte(VBB_DAT_Queue * Queue, char Byte)
{
    Queue->buffer[Queue->tail & Queue->bufferSizeMinusOne] = Byte;
    Queue->tail++;
}

char vbb_DataQueue_NextByte(VBB_DAT_Queue * Queue)
{
    if (Queue->head < Queue->tail)
    {
        char c = Queue->buffer[Queue->head & Queue->bufferSizeMinusOne];
        Queue->head++;
        return c;
    }
    return 0;
}

/*
void vbb_MsgQueue_Init(VBB_MSG_Queue * Queue)
{
    vbb_DataQueue_Init(&(Queue->data));
    Queue->head = Queue->tail = 0;
}

void vbb_MsgQueue_AddMsg(VBB_MSG_Queue * Queue, const char * Data)
{   
    if (Data[0] != 0)
    {   
        while (Data[0])
        {
            vbb_DataQueue_AddByte(&(Queue->data), Data[0]);
            Data++;
        }
        vbb_DataQueue_AddByte(&(Queue->data), 0);
        Queue->tail++;
    }
}

int vbb_MsgQueue_Size(VBB_MSG_Queue * Queue)
{
    return (Queue->tail - Queue->head);
}

bool vbb_MsgQueue_NextMsg(VBB_MSG_Queue * Queue, char * Data)
{
    unsigned int count = 0;
    char c;
    Data[count] = 0;
    if (Queue->head < Queue->tail)
    {        
        c = vbb_DataQueue_NextByte(&(Queue->data));
        while (c)
        {
            Data[count] = c;
            count++;
            c = vbb_DataQueue_NextByte(&(Queue->data));
        }
        Data[count] = 0;
        
        Queue->head++;
        return TRUE;
    }   
    return FALSE;
}
*/
